/**
 * RenweiWu
 * PubDriverService.java
 * 2017年9月19日
 * PubDriverService
 * TODO
 * 
 */
package com.etrans.web.gps.business.base.service;

import java.text.ParseException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.etrans.web.gps.base.service.BaseService;
import com.etrans.web.gps.business.base.domain.PubDriver;
import com.etrans.web.gps.business.base.domain.PubDrivervo;
import com.etrans.web.gps.common.page.PageVo;
import com.etrans.web.gps.util.DateUtil;
import com.etrans.web.gps.util.WebServiceUtil;

/**
 * @Description:TODO
 * @author RenweiWu
 * @date 2017年9月19日
 * @ClassName:  PubDriverService  
 * @param:   
 */
@Service
public class PubDriverService extends BaseService{
	
	
	public void querOrganzation(){
		
	}
	
	public boolean addpubdriver(PubDrivervo vo){
		boolean result = false;
		PubDriver driver = getPubDriver(vo);
		int i = this.insert("pubDriverMapper.addpubDriver", driver);
		if(i==1)
			result =true;
		return result;
	}
	
	public List<HashMap<String,Object>>  queryOrganzation(String id){
		HashMap<String,Object> map = new HashMap<String,Object>();
		if(id!=null&&!"".equals(id))
			map.put("id",Integer.parseInt(id));
		return this.selectList("pubDriverQuery.queryOrganzation",map);
	}
	
	public PageVo queryPubDriver(Map<String,Object> maps){
		PageVo pagevo = new PageVo();
		List<PubDriver> lists = this.selectList("pubDriverQuery.queryPubDriver",maps);
		int count = selectOne("pubDriverQuery.getcount",maps);
			pagevo.setTotal(count);
			pagevo.setRows(lists);
			pagevo.setPage((Integer) maps.get("page"));	
		return pagevo;
	}
	
	public PubDriver queryOneById(Integer id){
		HashMap<String,Object> maps = new HashMap<String,Object>();
		maps.put("id",id);
		return selectOne("pubDriverQuery.queryOneById",maps);
	}
	
	public  boolean updatePubDriver(PubDrivervo vo){
		boolean result = false;
		PubDriver driver = this.getPubDriver(vo);
		this.update("pubDriverMapper.updatePubDriver",driver);
		result=true;
		return result;
	} 

	private PubDriver getPubDriver(PubDrivervo vo){
		PubDriver driver = new PubDriver();
		try {
			if(vo.getAcquire_time()!=null&&!"".equals(vo.getAcquire_time())){
				driver.setAcquire_time(DateUtil.getStrToDate(vo.getAcquire_time()));
			}
			if(vo.getGrant_time()!=null&&!"".equals(vo.getGrant_time())){
				driver.setGrant_time(DateUtil.getStrToDate(vo.getGrant_time()));
			}
			if(vo.getInvalid_end_time()!=null&&!"".equals(vo.getInvalid_end_time())){
				driver.setInvalid_end_time(DateUtil.getStrToDate(vo.getInvalid_end_time()));
			}
			if(vo.getInvalid_start_time()!=null&&!"".equals(vo.getInvalid_start_time())){
				driver.setInvalid_start_time(DateUtil.getStrToDate(vo.getInvalid_start_time()));
			}
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		driver.setLicence(vo.getLicence());
		if(vo.getEnable_sms()!=null&&!"".equals(vo.getEnable_sms())){
			driver.setEnable_sms(Integer.parseInt(vo.getEnable_sms()));
		}
		if(vo.getEnprise_id()!=null&&!"".equals(vo.getEnprise_id())){
			driver.setEnterprise_id(Integer.parseInt(vo.getEnprise_id()));
		}
		if(vo.getHas_sync()!=null&&!"".equals(vo.getHas_sync())){
			driver.setHas_sync(Integer.parseInt(vo.getHas_sync()));
		}
		if(vo.getId()!=null&&!"".equals(vo.getId())){
			driver.setId(Integer.parseInt(vo.getId()));
		}
		driver.setId_card(vo.getId_card());
		driver.setImg_url(vo.getImg_url());
		if(vo.getLssuing_auth_id()!=null&&!"".equals(vo.getLssuing_auth_id())){
			driver.setLssuing_auth_id(Integer.parseInt(vo.getLssuing_auth_id()));
		}
		driver.setName(vo.getName());
		driver.setPhone(vo.getPhone());
		driver.setDriver_licence(vo.getDriver_licence());
		driver.setDrive_type(vo.getDrive_type());
		if(vo.getStatus()!=null && !"".equals(vo.getStatus())){
			driver.setStatus(Integer.parseInt(vo.getStatus()));
		}
		if(vo.getVehicle_id()!=null && !"".equals(vo.getVehicle_id())){
			driver.setVehicle_id(Integer.parseInt(vo.getVehicle_id()));
		}
		return driver;
	}
	
	public  boolean deleteDriver(List<Integer> list){
		boolean result = false;
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("id",list);
		this.delete("pubDriverMapper.deleteDriver",map);
		result=true;
		return result;
	}
	/**
	 * 同步运政从业人员信息
	 * @param yhmc
	 * @param xkzh
	 * @param ziparea
	 */
	public boolean synDriverInfo(String xm, String sfzh,String ziparea) {
		boolean flag = true;
		Map<String, Object> param = new HashMap<>();
		try{
			param =WebServiceUtil.getDriverInfoXml(xm,sfzh,ziparea);
//			String xmlDriver = "<?xml version='1.0' encoding='utf-8'?><soap:Envelope xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema'><soap:Body><GetDriverInfoResponse xmlns='http://tempuri.org/'><GetDriverInfoResult><xs:schema id='GetDriverInfo' xmlns='' xmlns:xs='http://www.w3.org/2001/XMLSchema' xmlns:msdata='urn:schemas-microsoft-com:xml-msdata'><xs:element name='GetDriverInfo' msdata:IsDataSet='true' msdata:MainDataTable='Employee' msdata:UseCurrentLocale='true'><xs:complexType><xs:choice minOccurs='0' maxOccurs='unbounded'><xs:element name='Employee'><xs:complexType><xs:sequence><xs:element name='FileNO' type='xs:string' minOccurs='0' /><xs:element name='Name' type='xs:string' minOccurs='0' /><xs:element name='IDCard' type='xs:string' minOccurs='0' /><xs:element name='Address' type='xs:string' minOccurs='0' /><xs:element name='Qualification' type='xs:string' minOccurs='0' /><xs:element name='QualificationID' type='xs:string' minOccurs='0' /><xs:element name='QualifiedDate' type='xs:string' minOccurs='0' /><xs:element name='QualifiedStartDate' type='xs:string' minOccurs='0' /><xs:element name='QualifiedEndDate' type='xs:string' minOccurs='0' /><xs:element name='ID_ECERTAREA' type='xs:string' minOccurs='0' /><xs:element name='QualificationState' type='xs:string' minOccurs='0' /></xs:sequence></xs:complexType></xs:element></xs:choice></xs:complexType></xs:element></xs:schema><diffgr:diffgram xmlns:msdata='urn:schemas-microsoft-com:xml-msdata' xmlns:diffgr='urn:schemas-microsoft-com:xml-diffgram-v1'><GetDriverInfo xmlns=''><Employee diffgr:id='Employee1' msdata:rowOrder='0' diffgr:hasChanges='inserted'><FileNO /><Name>隔壁老王</Name><IDCard>431224198802198709</IDCard><Address>湖南省溆浦县低庄镇严家坡村四组</Address><Qualification>道路危险货物运输押运人员</Qualification><QualificationID>431224198802198709</QualificationID><QualifiedDate>2015/11/11 10:21:52</QualifiedDate><QualifiedStartDate>2015/11/11 10:21:52</QualifiedStartDate><QualifiedEndDate>2021/11/10 0:00:00</QualifiedEndDate><ID_ECERTAREA>4501000000085245</ID_ECERTAREA><QualificationState>从业</QualificationState></Employee></GetDriverInfo></diffgr:diffgram></GetDriverInfoResult><ErrMsg /></GetDriverInfoResponse></soap:Body></soap:Envelope>";
//			ParseXml parse = new ParseXml();
//			param = parse.parse(xmlDriver);
			if(param.get("Name")!=null){
				this.update("pubDriverMapper.synDriverInfo", param);
			}else{
				flag = false;
			}
			return flag;
		}catch(Exception e){
			e.printStackTrace();
			return false;
		}	
		
	}

	public Map<String, Object> queryDriverVoById(Integer id) {
		
		return this.selectOne("pubDriverQuery.queryDriverVo", id);
	}
	
}
